var connection = new RTCMultiConnection();

   // this line is VERY_important
   connection.socketURL = 'https://blog.lqcpws.cn/';
// connection.dontGetRemoteStream = true;
connection.maxParticipantsAllowed = 2;
connection.direction = 'one-to-one';
   // if you want text chat
   connection.session = {
   	data: true
   };
   connection.enableLogs=false
   connection.enableFileSharing = true;
   connection.filesContainer = document.getElementById('files-container');

   // connection.onRoomFull = function(roomid) {
   // 	alert('Room is full.');
   // };
connection.onmessage = function(event) {
	// console.log(event)
    // alert(event.userid + ' said: ' + event.data);
	// let msg=event.userid+':'+event.data+'<br/>'
	// document.getElementById('content').innerHTML +=msg
	
};
   connection.onFileStart = function(data) {
	   app.disable=true
   	// console.log('onFileStart', data)
   }
   connection.onFileProgress = function(data) {
	   if(data.currentPosition>data.maxChunks){
		   setTimeout(function(){
			   app.percentage=0
		   },1500)
		   
	   }else{
	   app.percentage=parseInt(data.currentPosition/data.maxChunks*100)
	   }
   	// console.log('onFileProgress', data)
   }
   connection.onFileEnd = function(data) {
	    app.disable=false
		 setTimeout(function(){
		 			   app.percentage=0
		 },1500)
   	// console.log('onFileEnd', data)

	// data.size=formatFileSize(data.size)
	app.tableData.push(data)
   }
   connection.onerror = function(event) {
   	// alert('unable to open data connection between you and ' + event.userid);
   	// alert('发生未知异常，请稍后使用！')
	// console.log(event)
   }
   // connection.onclose=function(event){
	  //    app.disable=false
		 // app.percentage=0
	  //  app.$notify({
	  //  	title: '提示',
	  //  	message: '有用户已经断开，传输中断！',
	  //  	duration: 2000,
	  //  	type: 'warning'
	  //  })
	  //  console.log('用户离开',event)
	  //  // console.log(connection.peers.getLength())
	  //  app.peerCount--

   // }
  //  function changeUserChannel(connection){
	 //   connection.peers.forEach(function(item){
		  
		//    setTimeout(function(){
		// 	   console.log(item.peer.sctp) 
		// 	   item.peer.sctp.maxMessageSize=1024*1024*10
		// 	    console.log(item.peer.sctp) 
		//    },1000)
		//     // item.peer.sctp.maxMessageSize=1024*1024*10
		
	 //   })
  //  }
  

   connection.onUserStatusChanged = function(event, dontWriteLogs) {
	   if(event.status=='online'){
		   // changeUserChannel(connection)
		   	app.peerCount=connection.peers.getLength()
	   }
	   if(event.status=='offline'){
		    // changeUserChannel(connection)
	   		  app.disable=false
	   		  		 app.percentage=0
	   		  app.$notify({
	   		  	title: '提示',
	   		  	message: '有用户已经断开',
	   		  	duration: 2000,
	   		  	type: 'warning'
	   		  })
	   		  // console.log('用户离开',event)
	   		  // console.log(connection.peers.getLength())
	   		  app.peerCount--
	   		   
	   }
	   // console.log('onUserStatusChanged',event)

	// app.peerCount+=1

	

   }

   // console.log(connection)
   connection.chunkSize=1024*255;
   connection.publicRoomIdentifier="lqcpws:"+app.roomId
   connection.iceServers = [{
                            urls: "stun:106.14.157.136:3478"
                        }, 
   						{
   						    urls: "turn:106.14.157.136:3478",
   						    username: "demo",
   						    credential: "123456"
   						}]
						
						// [{
						//                         urls: "stun:deershare.com"
						//                     }, {
						//                         urls: "turn:0.peerjs.com:3478",
						//                         username: "peerjs",
						//                         credential: "peerjsp"
						//                     }]
						connection.candidates = {
						    turn: true,
						    stun: true,
						    host: true
						};
						// connection.trickleIce=false 
						// to get both STUN and TURN candidate pairs
						// connection.iceTransportPolicy = 'relay';
   connection.openOrJoin(app.roomId,function(data1,data2,data3){
	   
	   console.log(data1,data2,data3)
	   if(!data1){
	   	alert('房间人数已满，即将返回主页面')
	   	window.location.href='index.html'
	   	
	   }
   });



   function sendFile(file) {

   	// file.uuid = connection.token();
	try{
	connection.peers.send(file)
	}catch(e){
		console.log(e)
	}
   	// console.log(file)

   	// 			console.log();
   	// 			console.log(connection)
   }
   // connection.candidates = {
   //     turn: false,
   //     stun: false,
   //     host: false
   // };
   // connection.iceTransportPolicy = 'all';

						
					
   function sendMsg(data) {
   
   	// file.uuid = connection.token();
   	try{
   	connection.peers.send(data)
   	}catch(e){
   		console.log(e)
   	}
   	// console.log(file)
   
   	// 			console.log();
   	// 			console.log(connection)
   }
